package com.penghaisoft.microwms.product.web.controller.outwarehouse;

import java.util.Date;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.data.model.ProSellOut;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.service.IWmsCommonService;
import com.penghaisoft.microwms.product.service.IProSellOutService;

@RestController
@RequestMapping(value = "/prosellout")
@Slf4j
public class ProSellOutController extends BaseController {

    @Autowired
    private IProSellOutService proSellOutService;
    
    @Autowired
	private IWmsCommonService wmsCommonService;

    /**
     * 新增记录
     *
     * @param proSellOut
     * @return
     */
    @PostMapping("c")
    public TransResult create(ProSellOut proSellOut) {
        proSellOut.preInsert();
        Resp resp = proSellOutService.create(proSellOut);
        return fromResp(resp);
    }

    /**
     * 删除记录（逻辑删除active_flag=0)
     *
     * @param proSellOut
     * @return
     */
    @PostMapping("d")
    public TransResult delete(ProSellOut proSellOut) {
        proSellOut.preUpdate();
        Resp resp = proSellOutService.delete(proSellOut);
        return fromResp(resp);
    }

    /*
     * 查询列表
     *
     * @param page,rows
     * @return
     * */
    @PostMapping("list")
    public TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
                            @RequestParam(name = "rows", defaultValue = "10") int rows, ProSellOut proSellOut) {
        Pager<ProSellOut> result = proSellOutService.findListByCondition(page, rows, proSellOut);
        return success(result);
    }

    /**
     * 查询单条
     *
     * @param id
     * @return
     */

    @GetMapping("r/{id}")
    public TransResult queryById(@PathVariable String id) {
        ProSellOut proSellOut = proSellOutService.findById(id);
        return success(proSellOut);
    }


    /* * 修改记录
     *
     * @param proSellOut
     * @return
     */
    @PostMapping("u")
    public TransResult update(ProSellOut proSellOut) {
        proSellOut.preUpdate();
        Resp resp = proSellOutService.update(proSellOut);
        return fromResp(resp);
    }

    /**
     * @Description 成品销售出库单创建
     * @copy luoteng
     * @param list
     * @return TransResult
     */
    @PostMapping("insert")
    public TransResult proSellOutCreate(@RequestBody List<ProSellOut> list) {
        TransResult transResult = new TransResult();
        if(list == null || list.isEmpty()){
            transResult.setCode(Constant.FAIL);
            transResult.setMsg("请选择成品型号及数量！");
            return transResult;
        }else{
        	String sellOutNo = wmsCommonService.getProShortSeq5(Constant.SEQ_PREFIX_SHORT_XSC, Constant.SEQ_PREFIX_SHORT_REDIS_PRO_XSC, 1).get(0);
            Resp resp = proSellOutService.insert(list, sellOutNo, getLoginName());
            transResult.setCode(resp.getCode());
            transResult.setMsg(resp.getMsg());
        }

        return transResult;
    }
    
    /** 
     * @Title proSellOutUpdate
     * @Description 销售出库单 单条修改
     * @author luoteng
     * @date 2018年6月29日:下午1:29:39
     * @param proSellOut
     * @return TransResult
     */
    @PostMapping("update")
	public TransResult proSellOutUpdate(ProSellOut proSellOut) {
		TransResult transResult = new TransResult();
		transResult.setCode(Constant.SUCCESS);
		ProSellOut ob = new ProSellOut();
		ob.setActiveFlag("1");
		ob.setSellOutId(proSellOut.getSellOutId());

		List<ProSellOut> list  = proSellOutService.queryListByAny(ob);
		if(list != null && !list.isEmpty()){
			ProSellOut tmp = list.get(0);
//			0新建 1入库中 2入库完成
			if(tmp.getSellOutStatus() != null && !tmp.getSellOutStatus().equals("0")){
				transResult.setCode(Constant.FAIL);
				transResult.setMsg("该销售出库单状态为出库中或者出库完成，不能修改！");
				return transResult;
			}
		}
		
		proSellOut.setLastModifiedBy(getLoginName());
		proSellOut.setGmtModified(new Date());
		proSellOutService.update(proSellOut);
		
		return transResult;
	}
	
	/** 
	 * @Title getProSellOutIdList
	 * @Description 根据销售出库单号查询列表
	 * @author luoteng
	 * @date 2018年6月29日:下午1:23:11
	 * @param sellOutId
	 * @return TransResult
	 */
	@GetMapping("get/id/{sellOutId}")
	public TransResult getProSellOutIdList(@PathVariable("sellOutId")String sellOutId) {
		TransResult transResult = new TransResult();
		transResult.setCode(Constant.SUCCESS);
		
		ProSellOut ob = new ProSellOut();
		ob.setActiveFlag("1");
		ob.setSellOutId(Integer.parseInt(sellOutId));

		List<ProSellOut> list  = proSellOutService.queryListByAny(ob);
		if(list != null && !list.isEmpty()){
			ProSellOut tmp = list.get(0);
//			0新建 1入库中 2入库完成 3审核通过 4审核不通过'
			if(tmp.getSellOutStatus() == null){
				transResult.setCode(Constant.FAIL);
				transResult.setMsg("该销售出库单状态未知！");
				return transResult;
			}
			if(tmp.getSellOutStatus() != null && !tmp.getSellOutStatus().equals("0")){
				transResult.setCode(Constant.FAIL);
				transResult.setMsg("该销售出库单不能修改！");
				return transResult;
			}
			transResult.setData(tmp);
		}
		
		return transResult;
	}
	
	/**
	 * 审核
	 * @return
	 */
	@PostMapping("audit")
	public TransResult proSellOutAudit(ProSellOut proSellOut){
		TransResult result = new TransResult();
    	proSellOut.setLastModifiedBy(getLoginName());
    	proSellOut.setGmtModified(new Date());
    	
    	ProSellOut pro = new ProSellOut();
    	pro.setActiveFlag("1");
//    	pro.setSellOutNo(proSellOut.getSellOutNo());
    	pro.setSellOutId(proSellOut.getSellOutId());
		
    	List<ProSellOut> list = proSellOutService.queryListByAny(pro);
    	if(list !=null && !list.isEmpty()){
//			0新建 1出库中 2出库完成 3审核通过 4审核不通过
			if(!list.get(0).getSellOutStatus().equals("0")){
				result.setCode(Constant.FAIL);
				result.setMsg("该类型销售出库单不能审核！");
    	        return result;
    		}
    	}
    	//更新时，只改出库状态是  新建的
    	proSellOut.setStatus("0");
    	if(proSellOut.getSellOutStatus().equals("4")){
    		proSellOut.setActiveFlag(Constant.FAIL);
    	}
    	Resp resp = proSellOutService.updateByNo(proSellOut);
    	result.setCode(resp.getCode());
    	result.setMsg(resp.getMsg());
		return result;
		
	}
	@PostMapping("audit/alone")
	public TransResult proSellOutAuditAlone(ProSellOut proSellOut){
		TransResult result = new TransResult();
    	proSellOut.setLastModifiedBy(getLoginName());
    	proSellOut.setGmtModified(new Date());
    	
    	ProSellOut pro = new ProSellOut();
    	pro.setActiveFlag("1");
//    	pro.setSellOutNo(proSellOut.getSellOutNo());
    	pro.setSellOutId(proSellOut.getSellOutId());
    	
		
    	List<ProSellOut> list = proSellOutService.queryListByAny(pro);
    	if(list !=null && !list.isEmpty()){
//			0新建 1出库中 2出库完成 3审核通过 4审核不通过
			if(!list.get(0).getSellOutStatus().equals("0")){
				result.setCode(Constant.FAIL);
				result.setMsg("该类型销售出库单不能审核！");
    	        return result;
    		}
    	}
    	if(proSellOut.getSellOutStatus().equals("4")){
    		proSellOut.setActiveFlag(Constant.FAIL);
    	}
    	Resp resp = proSellOutService.update(proSellOut);
    	result.setCode(resp.getCode());
    	result.setMsg(resp.getMsg());
		return result;
		
	}
}
